#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "Article.h"
#include <sstream>
#include <fstream>
using namespace std;

class ArticleRegister
{
private:
	vector<Article> vaRegister;

public:
	// konstruktor
	ArticleRegister(void);

	//metoder
	void Add(Article article)
	{
		vaRegister.push_back(article);
	}

	string List()
	{
		ostringstream oss;
		for (auto &a : vaRegister)
		{
			oss << a << endl;
		}


		return oss.str();
	}

	void WriteToFile(string fileName)
	{
		ofstream fout(fileName);
		for (auto &a : vaRegister)
		{
			fout << "Article: " << a << endl;
		}
		fout.close();
	}

	void ReadFromFile(string fileName)
	{
		string str, strRow;
		ifstream fin;

		fin.open(fileName);
		vaRegister.clear();

		while (getline(fin, strRow))
		{
			istringstream iss(strRow);
			getline(iss, str, ':');
			if (str.compare("Article") == 0)
			{
				Article a;
				iss >> a;
				vaRegister.push_back(a);
			}
		}
		fin.close();
	}

	double CalculateTotalValue()
	{
		double dTotalPrice = 0;
		for (auto &a : vaRegister)
		{
			dTotalPrice += a.GetPrice();
		}
		return dTotalPrice;
	}

	// destruktor
	~ArticleRegister(void);
};

